/*
 * Copyright (c) 2024 Black Sesame Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gtest/gtest.h>
#include "ndds/ndds_c.h"
#include "dal/domain_participant.h"
#include "dal_rticonnextdds/entity_impl/DalDomainParticipantImpl.hpp"
#include "dal_rticonnextdds/converter/GuidConverter.hpp"

TEST(ConvertGuidFromRtiddsToDal, GuidConverter)
{
    dal_domain_participant_t* participant = nullptr;
    participant =
        dal_create_domain_participant(20, DAL_PARTICIPANT_QOS_DEFAULT, nullptr, DAL_ANY_STATUS);
    EXPECT_NE(participant, nullptr);

    DDS_DomainParticipant* r_participant = nullptr;
    r_participant = (reinterpret_cast<DalDomainParticipantImpl*>(participant->handle_))
                        ->RtiddsDomainParticipant();
    EXPECT_NE(r_participant, nullptr);

    DDS_InstanceHandle_t r_ins =
        DDS_Entity_get_instance_handle(DDS_DomainParticipant_as_entity(r_participant));

    DDS_GUID_t r_guid;
    memcpy(r_guid.value, r_ins.keyHash.value, GUID_SIZE);

    dal_guid_t d_guid = dal_domain_participant_get_guid(participant);

    auto ret = memcmp(r_guid.value, d_guid.guidprefix.value, GUIDPREFIX_SIZE);
    EXPECT_EQ(0, ret);

    ret = memcmp(r_guid.value + GUIDPREFIX_SIZE, d_guid.entity_id.value, ENTITYID_SIZE);
    EXPECT_EQ(0, ret);
}
